package com.soundShape.mediators.howTo
{
	
	import com.soundShape.constants.SoundShapesInfo;
	import com.soundShape.events.SoundShapeEvent;
	import com.soundShape.mediators.AbstractViewMediator;
	import com.soundShape.views.howTo.HowTo;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.LinkButton;
	import mx.events.EffectEvent;
	import mx.events.FlexEvent;
	import mx.states.State;
	
	import org.osmf.events.TimeEvent;
	
	import spark.components.Button;
	import spark.effects.Fade;
	import spark.effects.Move;
	import spark.effects.Resize;
	import spark.utils.TextFlowUtil;
	
	
	
	public class HowToMediator extends AbstractViewMediator
	{
		
				
				
				public function HowToMediator()
				{
					super(HowTo);
				}
				
				/**
				 * Since the AbstractViewMediator sets the view via Autowiring in Swiz,
				 * we need to create a local getter to access the underlying, expected view
				 * class type.
				 */
				public function get view():HowTo
				{
					return this._view as HowTo;
				}
				
				/**
				 * The <code>init()</code> method is fired off automatically by the 
				 * AbstractViewMediator when the creation complete event fires for the
				 * corresponding ViewMediator's view. This allows us to listen for events
				 * and set data bindings on the view with the confidence that our view
				 * and all of it's child views have been created and live on the stage.
				 */
				override protected function init():void
				{
					super.init();
					
					this.setViews();
					
				}
				
				override protected function setViewDataBindings():void
				{
					
				}	
				
				override protected function setViewListeners():void
				{
					super.setViewListeners();
					
					// link buttons
					this.view.lnkbtnLogin.addEventListener(MouseEvent.CLICK, handleLinkButton);
					
					this.view.lnkbtnNewCustomer.addEventListener(MouseEvent.CLICK, handleLinkButton);
					
					this.view.lnkbtnSpecialDate.addEventListener(MouseEvent.CLICK, handleLinkButton);
					
					this.view.lnkbtnAddReferral.addEventListener(MouseEvent.CLICK, handleLinkButton);
					
					this.view.lnkbtnAddTreatment.addEventListener(MouseEvent.CLICK, handleLinkButton);
					
					this.view.lnkbtnMngPhoto.addEventListener(MouseEvent.CLICK, handleLinkButton);
					
					this.view.lnkbtnCompare.addEventListener(MouseEvent.CLICK, handleLinkButton);
					
					this.view.lnkbtnUpdateCustomer.addEventListener(MouseEvent.CLICK, handleLinkButton);
					
					this.view.lnkbtnDeleteCustomer.addEventListener(MouseEvent.CLICK, handleLinkButton);
					
					this.view.lnkbtnUpdateEmployee.addEventListener(MouseEvent.CLICK, handleLinkButton);
					
					this.view.lnkbtnDeleteEmployee.addEventListener(MouseEvent.CLICK, handleLinkButton);
					
					this.view.lnkbtnUpdateReferral.addEventListener(MouseEvent.CLICK, handleLinkButton);
					
					this.view.lnkbtnUpdateSpecialDate.addEventListener(MouseEvent.CLICK, handleLinkButton);
					
					this.view.lnkbtnPractice.addEventListener(MouseEvent.CLICK, handleLinkButton);
					
					this.view.lnkbtnControlVideo.addEventListener(MouseEvent.CLICK, handleLinkButton);
					
					//other stuff
					this.view.bcStartScreen.addEventListener(MouseEvent.CLICK, handleBcStartScreenClick);
				}
				
							
							
				
				protected function handleLinkButton(event:MouseEvent):void
				{
					var id:String = LinkButton(event.currentTarget).id;
					
					switch(id)
					{
						case "lnkbtnLogin":
							isVideoStart = true;
							doVideoWindow(null,"login");
							break;
						
						case "lnkbtnNewCustomer":
							isVideoStart = true;
							doVideoWindow(null,"newCustomer");
							break;
						
						case "lnkbtnSpecialDate":
							isVideoStart = true;
							doVideoWindow(null,"specialDate");
							break;
						
						case "lnkbtnAddReferral":
							isVideoStart = true;
							doVideoWindow(null,"addReferral");
							break;
						
						case "lnkbtnAddTreatment":
							isVideoStart = true;
							doVideoWindow(null,"addTreatment");
							break;
						
						case "lnkbtnMngPhoto":
							isVideoStart = true;
							doVideoWindow(null,"mngPhoto");
							break;
						
						case "lnkbtnCompare":
							isVideoStart = true;
							doVideoWindow(null,"compare");
							break;
						
						case "lnkbtnUpdateCustomer":
							isVideoStart = true;
							doVideoWindow(null,"updateCustomer");
							break;
						
						case "lnkbtnDeleteCustomer":
							isVideoStart = true;
							doVideoWindow(null,"deleteCustomer");
							break;
						
						case "lnkbtnUpdateEmployee":
							isVideoStart = true;
							doVideoWindow(null,"updateEmployee");
							break;
						
						case "lnkbtnDeleteEmployee":
							isVideoStart = true;
							doVideoWindow(null,"deleteEmployee");
							break;
						
						case "lnkbtnUpdateReferral":
							isVideoStart = true;
							doVideoWindow(null,"updateReferral");
							break;
						
						case "lnkbtnUpdateSpecialDate":
							isVideoStart = true;
							doVideoWindow(null,"updateSpecialDate");
							break;
						
						case "lnkbtnControlVideo":
							isVideoStart = true;
							doVideoWindow(null,"controlVideo");
							break;
						
						case "lnkbtnPractice":
							doPractice(true);
							break;
					}
				}
				
				
				
				private function setViews():void
				{
					//this.view.currentState = "start";
				}
				
				
				/**
				 * *******************************************************************************
				 * Handlers
				 * *******************************************************************************
				 */
				private function doPractice(isPractice:Boolean):void
				{
					this.view.currentState = "practice";
					this.view.btnStartPractice.addEventListener(MouseEvent.CLICK, handlePractice);
					this.view.btnStopPractice.addEventListener(MouseEvent.CLICK, handlePractice);
					this.view.txtAreaPractice.textFlow = TextFlowUtil.importFromString(SoundShapesInfo.practiceInfo);
				}
				
				protected function handlePractice(event:Event):void
				{
					var soundShapeEvent:SoundShapeEvent = new SoundShapeEvent(SoundShapeEvent.SHOW_PRACTICE_MODE);
					
					var id:String = Button(event.currentTarget).id;
					
					if(id == "btnStartPractice")
						soundShapeEvent.data = true;
					else
						soundShapeEvent.data = false;
					
					this.view.btnStartPractice.enabled = !soundShapeEvent.data;
					this.view.btnStopPractice.enabled = soundShapeEvent.data;
					
					this.dispatcher.dispatchEvent(soundShapeEvent);
					
					doUsingLabelFade(id);
				}
				
				private function doUsingLabelFade(id:String):void
				{
					
					this.view.bcPractice.alpha = 0.3;
					
					var fade:Fade = new Fade();
					
					if(id == "btnStartPractice")
					{
						fade.target = this.view.lblUsingPractice;
						this.view.lblUsingPractice.visible = true;
					}
					else
					{
						fade.target = this.view.lblUsingProduction;
						this.view.lblUsingProduction.visible = true;
					}
					
					fade.alphaFrom = 1.0;
					fade.alphaTo = 0.0;
					fade.duration = 2000;
					fade.addEventListener(EffectEvent.EFFECT_END, handleFadeEnd);
					fade.play();
					
				}				
				
				protected function handleFadeEnd(event:EffectEvent):void
				{
					this.view.bcPractice.alpha = 1.0;					
				}
				
				protected function handleBcStartScreenClick(event:MouseEvent):void
				{
					this.view.greenArrow.doGreenArrow();
					this.view.grnArw.doGreenArrow();
				}	
				
				
				
				/**
				 * *******************************************************************************
				 * Swiz EventHanders
				 * *******************************************************************************
				 */
				[EventHandler(event="SoundShapeEvent.RESET_VIDEO")]
				public function doVideoReset(soundShapeEvent:SoundShapeEvent=null):void
				{
					try
					{
						this.view.vdHowTo.stop();
						this.view.vdHowTo.source = null;
					}
					catch(e:Error){}
					
					doVideoWindow();
				}
				
				/**
				 * *******************************************************************************
				 * util
				 * *******************************************************************************
				 */
				
				private var isVideoStart:Boolean;
				private var videoSubject:String;
				
				private function doVideoWindow(timeEvent:TimeEvent=null, subject:String=null):void
				{
					
					if(isVideoStart)
						this.view.currentState = "video";
					else
					{
						if(this.view != null)
							this.view.currentState = "start";
						return;
					}
						
					
					videoSubject = subject;
					
					var resize:Resize = new Resize();
						resize.target = this.view.bcVideo;
						resize.heightTo = (isVideoStart)?696:396;
						resize.widthTo = (isVideoStart)?996:696;
						resize.duration = 500;
						resize.play();
						
					var fade:Fade = new Fade();
						fade.target = this.view.bcBlankcreen;
						fade.alphaTo = (isVideoStart)?0.0:1.0;
						fade.duration = 1000;
						fade.play();
						
					var move:Move = new Move();
						move.target = this.view.bcVideo;
						move.xTo = (isVideoStart)?1:257;
						move.yTo = (isVideoStart)?1:137;
						move.duration = 500;
						(isVideoStart)?move.addEventListener(EffectEvent.EFFECT_END, handleMoveEnd):this.view.vdHowTo.source=null;
						move.play();
					
				}
				
				protected function handleMoveEnd(event:EffectEvent):void
				{
					
					switch(videoSubject)
					{
						case "login":
							this.view.vdHowTo.source = "assets/video/login.f4v";
							break;
						
						case "newCustomer":
							this.view.vdHowTo.source = "assets/video/newCustomer.f4v";
							break;
						
						case "addReferral":
							this.view.vdHowTo.source = "assets/video/addReferral.f4v";
							break;
						
						case "addTreatment":
							this.view.vdHowTo.source = "assets/video/addReferral.f4v";
							break;
						
						case "mngPhoto":
							this.view.vdHowTo.source = "assets/video/addReferral.f4v";
							break;
						
						case "compare":
							this.view.vdHowTo.source = "assets/video/addReferral.f4v";
							break;
						
						case "updateCustomer":
							this.view.vdHowTo.source = "assets/video/addReferral.f4v";
							break;
						
						case "deleteCustomer":
							this.view.vdHowTo.source = "assets/video/addReferral.f4v";
							break;
						
						case "updateEmployee":
							this.view.vdHowTo.source = "assets/video/addReferral.f4v";
							break;
						
						case "deleteEmployee":
							this.view.vdHowTo.source = "assets/video/addReferral.f4v";
							break;
						
						case "updateReferral":
							this.view.vdHowTo.source = "assets/video/addReferral.f4v";
							break;
						
						case "updateSpecialDate":
							this.view.vdHowTo.source = "assets/video/addReferral.f4v";
							break;
						
						case "controlVideo":
							this.view.vdHowTo.source = "assets/video/addReferral.f4v";
							break;
					}
					
					this.view.vdHowTo.addEventListener(TimeEvent.COMPLETE, doVideoWindow);
					this.view.customVideoControl.addEventListener(SoundShapeEvent.RESET_VIDEO, doVideoReset);
					
					this.view.vdHowTo.play();
					
					isVideoStart = false;
					
					
				}				
				
		}
		
	}
